package com.example.cadmin.utils.Netty;

import cn.hutool.core.convert.Convert;
import io.netty.buffer.Unpooled;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 发送命令封装工具类
 */
@Component
public class sendCmd {

    public void send(String cmdName, HttpServletRequest request,String appointIp) {
        String sendIpAddress = "";
        if (request != null) {
            HttpSession session = request.getSession();
            sendIpAddress = Convert.toStr(session.getAttribute("cabinet"));
        } else {
            sendIpAddress = appointIp;
        }

        String finalSendIpAddress = sendIpAddress;
//        NettyServerHandler.channelGroup.forEach(channel -> {
//            System.out.println(channel.remoteAddress());
//            if (finalSendIpAddress.equals(channel.remoteAddress().toString())) {
//                try {
//                    System.out.println("已发送  "+"主机ip是  "+finalSendIpAddress +"命令为 "+cmdName + "\r\n");
//                    channel.writeAndFlush(Unpooled.buffer().writeBytes((cmdName + "\r\n").getBytes("gbk")));
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            } else {
//                System.out.println("未发送");
//            }
//        });
    }
}
